package com.jinmdz.fmis.api.wrapper;

import com.jinmdz.fmis.common.util.CamelCaseUtil;
import com.jinmdz.fmis.mapper.annotation.MyTable;
import com.jinmdz.fmis.mapper.base.BaseEntity;
import org.springframework.stereotype.Component;

/**
 * 通用工具函数封装出类
 *
 * @author LiCongLu
 * @date 2020-01-07 09:04
 */
@Component
public class ToolWrapper {
    /**
     * 获取实体所对应的表名称
     *
     * @param clazz 所代表的实体类
     * @return
     * @author LiCongLu
     * @date 2020-01-07 09:13
     */
    public <T extends BaseEntity> String getTableName(Class<T> clazz) {
        // 存在指定表名时
        if (clazz.isAnnotationPresent(MyTable.class)) {
            MyTable myTable = clazz.getAnnotation(MyTable.class);
            if (myTable != null && myTable.name() != null && myTable.name().length() > 0) {
                return myTable.name();
            }
        }
        // 不存在指定表名时
        return CamelCaseUtil.camelToUnderline(clazz.getSimpleName().replace("Entity", ""));
    }
}
